package com.lcz.mytest.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.lcz.mytest.R;
import com.lcz.mytest.db.UserService;
import com.lcz.mytest.domain.User;
import com.lcz.mytest.utils.SharedPrefsUtil;
import com.lcz.mytest.widget.ToolBarActivity;


public class RegistersActivity extends ToolBarActivity {

    private EditText name;
    private EditText psw;
    private EditText psw2;
    private Button btregister;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_regist);
        getSupportActionBar().setTitle("注册");
        name = (EditText) findViewById(R.id.et_regist_user);
        psw = (EditText) findViewById(R.id.et_regist_password);
        psw2 = (EditText) findViewById(R.id.et_regist_password2);
        btregister = (Button) findViewById(R.id.bt_register);
        btregister.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 获取填写内容
                final String username = name.getText().toString().trim();
                String password = psw.getText().toString().trim();
                String password2 = psw2.getText().toString().trim();
                // 对内容进行校验
                if (!(TextUtils.isEmpty(username)
                        && TextUtils.isEmpty(password) && TextUtils
                        .isEmpty(password2))) {
                    if (!password.equals(password2)) {
                        new AlertDialog.Builder(RegistersActivity.this)
                                .setTitle("提示").setMessage("两次密码不一致")
                                .setPositiveButton("确定", null).show()
                                .setCanceledOnTouchOutside(false);
                        psw2.setText("");
                    } else {
                        UserService uService = new UserService(
                                RegistersActivity.this);
                        final User user = new User();
                        user.setName(username);
                        user.setPassword(password);
                        // 注册校验
                        if (uService.register(user)) {
                            new AlertDialog.Builder(RegistersActivity.this)
                                    .setTitle("提示")
                                    .setMessage("注册成功")
                                    .setPositiveButton(
                                            "确定",
                                            new DialogInterface.OnClickListener() {

                                                @Override
                                                public void onClick(
                                                        DialogInterface dialog,
                                                        int which) {
                                                    SharedPrefsUtil.putValue(RegistersActivity.this, "username", user.getName());
                                                    SharedPrefsUtil.putValue(RegistersActivity.this, "isRemember", false);
                                                    Intent intent = new Intent();
                                                    setResult(RESULT_OK, intent);
                                                    finish();
                                                }
                                            }).show()
                                    .setCanceledOnTouchOutside(false);
                        } else {
                            new AlertDialog.Builder(RegistersActivity.this)
                                    .setTitle("提示").setMessage("注册失败")
                                    .setPositiveButton("确定", null).show()
                                    .setCanceledOnTouchOutside(false);
                        }
                    }
                } else {
                    new AlertDialog.Builder(RegistersActivity.this)
                            .setTitle("提示").setMessage("帐号密码不能为空")
                            .setPositiveButton("确定", null).show()
                            .setCanceledOnTouchOutside(false);
                }

            }
        });

    }

    public void cancle(View view) {
        finish();
    }

}